package com.yulongtian.weekCompetition.month202302.week03;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 第 333 场周赛
 *可
 * @author yulongTian
 * @create 2023-02-19 9:35
 */
public class Test01 {
    public static void main(String[] args) {
        int[][] nums1 = {{148, 597}, {165, 623}, {306, 359}, {349, 566}, {403, 646}, {420, 381}, {566, 543}, {730, 209}, {757, 875}, {788, 208}, {932, 695}};
        int[][] nums2 = {{74, 669}, {87, 399}, {89, 165}, {99, 749}, {122, 401}, {138, 16}, {144, 714}, {148, 206}, {177, 948}, {211, 653}, {285, 775}, {309, 289}, {349, 396}, {386, 831}, {403, 318}, {405, 119}, {420, 153}, {468, 433}, {504, 101}, {566, 128}, {603, 688}, {618, 628}, {622, 586}, {641, 46}, {653, 922}, {672, 772}, {691, 823}, {693, 900}, {756, 878}, {757, 952}, {770, 795}, {806, 118}, {813, 88}, {919, 501}, {935, 253}, {982, 385}};
        System.out.println(Arrays.deepToString(mergeArrays(nums1, nums2)));
    }

    public static int[][] mergeArrays(int[][] nums1, int[][] nums2) {
        List<int[]> temp_ans = new ArrayList<>();
        int point1 = 0;
        int point2 = 0;
        while (point1 < nums1.length && point2 < nums2.length) {
            if (nums1[point1][0] == nums2[point2][0]) {
                nums1[point1][1] += nums2[point2][1];
                temp_ans.add(nums1[point1]);
                point1++;
                point2++;
                continue;
            }
            if (nums1[point1][0] < nums2[point2][0]) {
                temp_ans.add(nums1[point1]);
                point1++;
                continue;
            }
            if (nums1[point1][0] > nums2[point2][0]) {
                temp_ans.add(nums2[point2]);
                point2++;
            }
        }

        if (point1 == nums1.length && point2 < nums2.length) {
            for (int i = point2; i < nums2.length; i++) {
                temp_ans.add(nums2[i]);
            }
        }
        if (point2 == nums2.length && point1 < nums1.length) {
            for (int i = point1; i < nums1.length; i++) {
                temp_ans.add(nums1[i]);
            }
        }

        int[][] ans = new int[temp_ans.size()][2];
        for (int i = 0; i < temp_ans.size(); i++) {
            ans[i][0] = temp_ans.get(i)[0];
            ans[i][1] = temp_ans.get(i)[1];
        }

        return ans;
    }

}
